import {Component, ViewChild} from '@angular/core';
import {Content, IonicPage, NavController, NavParams} from 'ionic-angular';
import {SideMenuService} from "../../../services/common/side-menu.service";
import {HistoricalPerformanceProvider} from "../../../providers/perfomance/historical-performance/historical-performance.provider";
import {LoadingService} from "../../../services/common/loading.service";
import {ToastService} from '../../../services/common/toast.service';

@IonicPage()
@Component({
    selector: 'page-historical-performance',
    templateUrl: 'historical-performance.html',
    providers: [
        SideMenuService,
        HistoricalPerformanceProvider,
        ToastService
    ]
})
export class HistoricalPerformancePage {
    //当前显示tab；
    private currentView = 0;
    private hisPerData;
    isScrolling = false;
    isLoaded:boolean = false;
    @ViewChild(Content) content: Content;
    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                private sideMenuService: SideMenuService,
                private hisPerProvider: HistoricalPerformanceProvider,
                public load:LoadingService,
                public toastService:ToastService
                ) {

    }

    ionViewDidLoad() {
        this.getGroupHistoricalPer(0)
    }

    /**
     * 回到顶部
     */
    goUp() {
        if (this.isScrolling) {
            setTimeout(() => {
                this.goUp();
            }, 300);
            return;
        }
        this.content.scrollToTop();
    }

    scrollStart(e) {
        this.isScrolling = true;
    }

    scrollEnd(e) {
        this.isScrolling = false;
    }

    /**
     * 获取团队历史业绩
     */
    getGroupHistoricalPer(id) {
        this.isLoaded = false;
        this.load.showLoading();
        this.hisPerProvider.getHistoricalPer(id).subscribe((res) => {
            this.hisPerData = res;
            this.isLoaded = true;
            this.load.dismissLoading();
        },err=>{
            this.load.dismissLoading();
        })
    }

    /**
     * 点击进入主管成员历史业绩列表
     */
    goToMemberPerformance(id) {
        if(this.currentView === 0){
            return;
        }
        this.navCtrl.push('MemberPerformancePage', {'groupId':id,"groupType":this.currentView});
    }

    /**
     * 点击tab切换
     */
    chooseView(num) {
        this.currentView = num;
        this.getGroupHistoricalPer(num);
    }

    /**
     * 打开菜单
     * @param myEvent
     */
    openMenu(myEvent) {
        this.sideMenuService.openSideMenu(myEvent)
    }

    /**
     * 返回
     * @param ev
     */
    backButtonClick(ev) {
        ev.preventDefault();
        ev.stopPropagation();
        this.navCtrl && this.navCtrl.pop(null, null);
    }

}
